{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from smoothnlp import config\n",
    "from smoothnlp import nlp\n",
    "config.setLogLevel(\"INFO\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 默认调用云服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 20句子\n",
    "config.POOL_TYPE='thread'\n",
    "texts = [\"SmoothNLP是一款优秀的开源项目\" for i in range(10)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Thread=2(默认)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:SmoothNLP:request parameter: NUM_THREAD = 2, POOL_TYPE = thread\n",
      "INFO:SmoothNLP:Request has been tried for 1 times\n",
      "INFO:SmoothNLP:Request has been tried for 2 times\n",
      "INFO:SmoothNLP:Request has been tried for 3 times\n",
      "INFO:SmoothNLP:Request has been tried for 4 times\n",
      "INFO:SmoothNLP:Request has been tried for 5 times\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 96.3 ms, sys: 20.2 ms, total: 116 ms\n",
      "Wall time: 1.39 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "results = nlp.analyze(texts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Thread = 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:SmoothNLP:request parameter: NUM_THREAD = 4, POOL_TYPE = thread\n",
      "INFO:SmoothNLP:Request has been tried for 1 times\n",
      "INFO:SmoothNLP:Request has been tried for 1 times\n",
      "INFO:SmoothNLP:Request has been tried for 1 times\n",
      "INFO:SmoothNLP:Request has been tried for 1 times\n",
      "INFO:SmoothNLP:Request has been tried for 2 times\n",
      "INFO:SmoothNLP:Request has been tried for 2 times\n",
      "INFO:SmoothNLP:Request has been tried for 2 times\n",
      "INFO:SmoothNLP:Request has been tried for 2 times\n",
      "INFO:SmoothNLP:Request has been tried for 3 times\n",
      "INFO:SmoothNLP:Request has been tried for 3 times\n",
      "INFO:SmoothNLP:Request has been tried for 3 times\n",
      "INFO:SmoothNLP:Request has been tried for 3 times\n",
      "INFO:SmoothNLP:Request has been tried for 4 times\n",
      "INFO:SmoothNLP:Request has been tried for 4 times\n",
      "INFO:SmoothNLP:Request has been tried for 4 times\n",
      "INFO:SmoothNLP:Request has been tried for 5 times\n",
      "INFO:SmoothNLP:Request has been tried for 4 times\n",
      "INFO:SmoothNLP:Request has been tried for 5 times\n",
      "INFO:SmoothNLP:Request has been tried for 5 times\n",
      "INFO:SmoothNLP:Request has been tried for 6 times\n",
      "INFO:SmoothNLP:Request has been tried for 5 times\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 148 ms, sys: 54.7 ms, total: 203 ms\n",
      "Wall time: 989 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "config.setNumThreads(4)\n",
    "results = nlp.analyze(texts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 调用本地部署的服务 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 某一本地部署环境\n",
    "config.setHost(\"http://172.17.0.1:28000\")\n",
    "config.setNLP_Path(\"/query\")\n",
    "## 500句子\n",
    "texts = [\"SmoothNLP是一款优秀的开源项目\" for i in range(500)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 多线程调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:SmoothNLP:request parameter: NUM_THREAD = 2, POOL_TYPE = thread\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.5 s, sys: 181 ms, total: 1.68 s\n",
      "Wall time: 9.8 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "config.setNumThreads(2)\n",
    "results = nlp.analyze(texts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:SmoothNLP:request parameter: NUM_THREAD = 8, POOL_TYPE = thread\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.15 s, sys: 139 ms, total: 1.29 s\n",
      "Wall time: 3.44 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "config.setNumThreads(8)\n",
    "results = nlp.analyze(texts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
